(function(window,undefined){
	var _callback ,_parentCategoryCode, _childCategoryCode, _categoryCode,_cntType;
	
	var Category = {};
	window.Category = window.Category || Category;
	
	/*페이지가 변경/시작될떄 호출*/
	Category.init = function(__callback,__cntType){
		var _landmark_code=sessionStorage.getItem('landmark_code');
		
		if(!__callback){
			alert('Category 콜백 초기화가 필요합니다.');
			return ;
		}
		
		if(!__cntType){
			__cntType = "storeCnt";
			return ;
		}
		_callback = __callback;
		_cntType = __cntType;
		
		_categoryCode = sessionStorage.getItem('category_code');
		if(!_categoryCode){
			_categoryCode = 'FO';
			sessionStorage.setItem('category_code',_categoryCode);
		}
		Category.loadCategoryCount(_landmark_code,__cntType);
		setParentCategoryCode();
	};
	Category.loadCategoryCount = function(_landmark_code,_cntType){
		$('#category > .depth1 > ul > li > .count > span').html(0);
		
		$.ajax({
			type:"post",
			url:ajaxURL + "category.php?action=loadCategoryCount",
			dataType:"json",
			data:{landmark_code:_landmark_code,cntType:_cntType},
			success:function(result){
				$.each(result,function(){
					$('#category > .depth1 > ul > li[data-praent_category_id="'+this.parent_code+'"] > .count > span').html(this.count);
				});
			}
		});
	}
	
	var setParentCategoryCode = function(){
		var _categoryCodeLangth = _categoryCode.length;
		
		if(_categoryCodeLangth == 2){
			_parentCategoryCode = _categoryCode;
		}else if(_categoryCodeLangth == 4) {
			_parentCategoryCode = _categoryCode.substring(0,2);
			_childCategoryCode = _categoryCode;
		}
	};
	
	Category.categoryBlock = function(){
		if(_parentCategoryCode){
			$('#category > .depth1 > ul > li').removeClass('on');
			$('#category > .depth1 > ul > li[data-praent_category_id="'+_parentCategoryCode+'"]').addClass('on');
			loadChildCategoryList(_cntType);
			_callback();
		}
		/*category parent listener*/
		$('#category > .depth1 > ul > li').off('click').on('click',function(event){
			event.preventDefault();
			
			_parentCategoryCode = $(this).data('praent_category_id');
			sessionStorage.setItem('category_code',_parentCategoryCode);
			_childCategoryCode = null;
			
			$('#category > .depth1 > ul > li').removeClass('on');
			$(this).addClass('on');
			
			loadChildCategoryList(_cntType);
			_callback();
		});
	};
	
	/*중분류 카테고리 가져오기*/
	var loadChildCategoryList = function(_cntType){
		var _landmark_code=sessionStorage.getItem('landmark_code');
		$.ajax({
			type:"post",
			url:ajaxURL + "category.php?action=loadChildCategoryList",
			dataType:"json",
			data:{parent_category_id:_parentCategoryCode,cntType:_cntType,landmark_code:_landmark_code},
			success:function(result){			
				$('#category > .depth2 > ul > li').parent().empty().append('<li data-category_code="'+_parentCategoryCode+'"><a href="#">전체</a></li>');
				$.each(result,function(){
					$('#category > .depth2 > ul').append('<li data-category_code="'+this.combine_code+'"><a href="#">'+this.name+'('+this.childCategoryCount+')</a></li>');
				});
				
				/*childeCategoryCode 가 있으면 유지하고 없으면 처음값에..*/
				if(_childCategoryCode){
					$('#category > .depth2 > ul > li[data-category_code="'+_childCategoryCode+'"]').addClass('on');
				} else {
					$('#category > .depth2 > ul > li:nth(0)').addClass('on');
				}
				
				/*category child listener*/
				$('#category > .depth2 > ul > li').off('click').on('click',function(event){
					event.preventDefault();
					
					$('#category > .depth2 > ul > li').removeClass('on');
					$(this).addClass('on');
					
					_childCategoryCode = $(this).data('category_code');
					sessionStorage.setItem('category_code',_childCategoryCode);
					
					_callback();
				});
			}
		});
	};
	
	/*대분류 카테고리에 상점 숫자 가져오기*/
	Category.loadStoreCountInCategory = function(){
		var _parent_category_id = "FO";
		
		$.ajax({
			type:"post",
			url:ajaxURL + "",
			dataType:"json",
			data:{},
			success:function(result){
			}
		});
	};
	
	Category.moveOverCategory = function(){
		if(!Account.isLogin()){
			return;
		} else {
			alert(NEED_LOGIN_MESSAGE);
		}
		
		var _user_id = 2;
		
		$.ajax({
			type:"post",
			url:ajaxURL + "",
			dataType:"json",
			data:{user_id:_user_id},
			success:function(result){
			}
		});
	};
})(window);
